<?php

/**
 * Created by PhpStorm.
 * role: shwy
 * Date: 2017/5/18
 * Time: 15:55
 * 用户管理模块
 */
class Role extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->model('role_model');
    }

    //权限组
    public function index(){
        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        $data = $this->role_model->page('role',0,$page_num,$start_position); //limit查询的数据
        $viewData = [
            'list' => $data[0],  //这里得到从数据库中的当页数据
            'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
            'pageNum'  => $page_num //每页显示多少条

        ];

        $this->load->view('admin/role/list',$viewData);
    }


    //添加页面
    public function addView(){

        $this->load->view('/admin/role/add');
    }

    //添加功能
    public function add(){

        $data = [
            'name' => $this->input->post('name'),
            'createTime' => time(),
        ];

        //是否会有添加失败的情况?

        $resultData =  $this->role_model->add($data);

        if($resultData){
            redirect('/admin/role/index');
        }
    }

    //编辑功能
    public function edit(){
        //用户id
        $where = [
            'id' => $this->input->post('id')
        ];
        //todo 更多字段
        $data = [
            'name' => $this->input->post('name'),
        ];

        //是否会有添加失败的情况?
        $resultData =  $this->role_model->edit($data,$where);

        if($resultData){
            redirect('/admin/role/index');
        }

    }

    //编辑页面
    public function editView(){

        //读取用户名根据get id
        $arg = [
            'id' => $this->input->get('id')
        ];

        $result['list'] = $this->role_model->getByFieldQueryData('id,name',$arg,0);

        $this->load->view('/admin/role/edit',$result);
    }

    //批量删除
    public function remove(){

        $postData = $this->input->post();

        if($postData){
            if(!$postData){
                redirect('/admin/role/index');
            }

            $post = $this->input->post('del_id');
            foreach ($post as $v){
                $where = [
                    'id' => $v
                ];

                $this->role_model->remove($where);
            }
            redirect('/admin/role/index');

        }else{
            $data = [
                'id' => $this->input->get('id'),
            ];

            //是否会有添加失败的情况?
            $resultData =  $this->role_model->remove($data);
            if($resultData){
                redirect('/admin/role/list');
            }
        }

    }


}